package org.jsola.contract.service.impl;

import org.jsola.contract.common.ExampleKit;
import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dao.IContractCredentialSiteDAO;
import org.jsola.contract.dto.ContractCredentialSiteAddDTO;
import org.jsola.contract.dto.ContractCredentialSiteUpdateDTO;
import org.jsola.contract.entity.ContractCredentialSiteDO;
import org.jsola.contract.query.ContractCredentialSiteQuery;
import org.jsola.contract.service.IContractCredentialSiteService;
import org.jsola.contract.vo.ContractCredentialSiteListVO;
import org.jsola.contract.vo.ContractCredentialSiteVO;
import org.jsola.core.Page;
import org.jsola.exception.ParamException;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 租户开通的电子合同账号
 *
 * @author JanusMix
 */
@Service("contractContractCredentialSiteServiceImpl")
public class ContractCredentialSiteServiceImpl implements IContractCredentialSiteService {

    @Autowired
    private IContractCredentialSiteDAO contractCredentialSiteDAO;

    @Override
    @Transactional(value = ContractConstants.TX, rollbackFor = Exception.class)
    public ContractCredentialSiteVO save(ContractCredentialSiteAddDTO contractCredentialSiteAddDTO, TokenUser tokenUser) {
        // 转DO
        ContractCredentialSiteDO contractCredentialSiteDO = contractCredentialSiteAddDTO.to(ContractCredentialSiteDO.class);
        ContractCredentialSiteDO existCredential = this.selectSiteCredential(tokenUser.getSiteId());
        ParamException.isNull(existCredential, "租户已经配置过合同凭证，请直接修改");
        // 保存
        contractCredentialSiteDO = save(contractCredentialSiteDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return contractCredentialSiteDO.to(ContractCredentialSiteVO.class);
    }

    @Override
    @Transactional(value = ContractConstants.TX, rollbackFor = Exception.class)
    public int update(ContractCredentialSiteUpdateDTO contractCredentialSiteUpdateDTO, TokenUser tokenUser) {
        //转DO
        ContractCredentialSiteDO contractCredentialSiteDO = contractCredentialSiteUpdateDTO.to(ContractCredentialSiteDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(contractCredentialSiteDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = ContractConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, Long... contractCredentialSiteIds) {
        return deleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) contractCredentialSiteIds);
    }


    @Override
    public ContractCredentialSiteVO selectById(Long contractCredentialSiteId, String siteId) {
        ContractCredentialSiteDO contractCredentialSiteDO = selectDOById(contractCredentialSiteId, siteId);
        if (contractCredentialSiteDO == null) {
            return null;
        }
        return contractCredentialSiteDO.to(ContractCredentialSiteVO.class);
    }

    @Override
    public List<ContractCredentialSiteListVO> select(ContractCredentialSiteQuery contractCredentialSiteQuery, String siteId) {
        List<ContractCredentialSiteDO> contractCredentialSiteDOList = selectDO(contractCredentialSiteQuery, siteId);
        if(CollectionUtils.isEmpty(contractCredentialSiteDOList)) {
            return contractCredentialSiteDOList == null ? null : new ArrayList<>();
        }
        return contractCredentialSiteDOList.stream()
                .map(contractCredentialSiteDO -> contractCredentialSiteDO.to(ContractCredentialSiteListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(ContractCredentialSiteQuery contractCredentialSiteQuery, String siteId) {
        Example example = buildExample(contractCredentialSiteQuery,siteId);
        return contractCredentialSiteDAO.selectCountByExample(example);
    }

    @Override
    public Page<ContractCredentialSiteListVO> selectPage(ContractCredentialSiteQuery contractCredentialSiteQuery, String siteId) {
        Example example = buildExample(contractCredentialSiteQuery,siteId);
        Page<ContractCredentialSiteDO> page = contractCredentialSiteDAO.selectPageByExample(example,
                contractCredentialSiteQuery.getPageNo(),
                contractCredentialSiteQuery.getPageSize());

        return page.to(ContractCredentialSiteListVO.class);
    }

    @Override
    public ContractCredentialSiteDO selectDOById(Long contractCredentialSiteId, String siteId) {
        return listById(contractCredentialSiteId, siteId);
    }

    @Override
    public List<ContractCredentialSiteDO> selectDO(ContractCredentialSiteQuery contractCredentialSiteQuery, String siteId) {
        Example example = buildExample(contractCredentialSiteQuery,siteId);
        return contractCredentialSiteDAO.selectByExample(example);
    }

    @Override
    public ContractCredentialSiteDO selectSiteCredential(String siteId) {
        Example example = new Example(ContractCredentialSiteDO.class);
        example.and()
                .andEqualTo("siteId", siteId);
        return contractCredentialSiteDAO.selectOneByExample(example);
    }

    @Override
    public void deleteByCredentialId(String credentialId, TokenUser tokenUser) {
        Example example = new Example(ContractCredentialSiteDO.class);
        example.and()
                .andEqualTo("siteId", tokenUser.getSiteId())
                .andEqualTo("contractCredentialId", credentialId);
        contractCredentialSiteDAO.deleteByExample(example);
    }

    @Override
    public ContractCredentialSiteDO selectByCredentialId(String credentialId, TokenUser tokenUser) {
        Example example = new Example(ContractCredentialSiteDO.class);
        example.and()
                .andEqualTo("siteId", tokenUser.getSiteId())
                .andEqualTo("contractCredentialId", credentialId);
        return contractCredentialSiteDAO.selectOneByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param contractCredentialSiteQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(ContractCredentialSiteQuery contractCredentialSiteQuery, String siteId) {
        Example example = new Example(ContractCredentialSiteDO.class);
        example.and()
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,contractCredentialSiteQuery.getOrders());
        return example;
    }
}




